//
//  AddCouponViewController.m
//  LvXi_newVersion
//
//  Created by 王敏 on 16/8/17.
//  Copyright © 2016年 SJT. All rights reserved.
//

#import "AddCouponViewController.h"

#import "AddCouponHeaderView.h"

#import "CouponCell.h"

@interface AddCouponViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)UITableView * tableView;

@property(nonatomic,strong)AddCouponHeaderView * headerView;

@property(nonatomic,strong)NSArray * couponList;


@property(nonatomic,strong)CouponModel * selectedCoupon;

@end

@implementation AddCouponViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = HexColor(@"#eeeeee");
    self.couponList = [NSArray array];
    [self.view addSubview:self.tableView];
    
    [[LXDataManager manager] getCouponListWithComplete:^(BOOL success, id res) {
        if(success){
            self.couponList = res;
            [self.tableView reloadData];
        }
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self setUpNavi];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}

-(void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    if(self.delegate&&[self.delegate respondsToSelector:@selector(AddCouponViewController:didSelectedCoupon:)]){
        [self.delegate AddCouponViewController:self didSelectedCoupon:self.selectedCoupon];
    }
}
#pragma mark -设置导航栏
-(void)setUpNavi{
    [super setUpNavi];
    self.navigationItem.title = @"添加优惠码";
}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1                                                                                                                                                                                      ;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.couponList.count;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CouponCell * cell = [tableView dequeueReusableCellWithIdentifier:@"CouponCell"];
    if(!cell){
        cell = [[CouponCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CouponCell"];
    }
    CouponModel * model = self.couponList[indexPath.row];
    cell.soonExpire = NO;
    cell.deductibleLabel.text = model.face_price;
    cell.thresholdLabel.text = model.ftypename;
    cell.dateLabel.text = model.f_endtime;
    [self addCellHeight:cell.cellHeight indexPath:indexPath];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [self cellHeightForIndexPath:indexPath];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return globalMargin;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    CouponModel * model = self.couponList[indexPath.row];
    self.selectedCoupon = model;
    [self.navigationController popViewControllerAnimated:YES];
}

-(UITableView*)tableView{
    if(_tableView  == nil){
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableHeaderView = self.headerView;
        _tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}

-(AddCouponHeaderView*)headerView{
    if(_headerView == nil){
        _headerView = [[AddCouponHeaderView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, Ratio_AddCoupon_Height(131))];
        _headerView.backgroundColor = [UIColor whiteColor];
    }
    return _headerView;
}


-(void)applyButtonClick:(UIButton*)aButton{
    UIView * containerView = [self.view viewWithTag:100];
//    for (UIView * v in containerView.subviews) {
//        [v removeFromSuperview];
//    }
    UIImageView * imgv = [[UIImageView alloc] initWithFrame:containerView.bounds];
    imgv.image = [UIImage imageNamed:@"default_usedcoupon"];
    [containerView addSubview:imgv];
    imgv.userInteractionEnabled = YES;
    [imgv addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(usedCouponClick:)]];
}

-(void)usedCouponClick:(UITapGestureRecognizer*)gesture{
    [gesture.view removeFromSuperview];
}
@end
